# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: creation_case.py
# Time: 16:47


import os
from common.utils.file_util import get_case_dir,create_file,mkdir
from common.tools.convert_ls import convert_str_to_hump
from common.utils.read_util import ReadFileData


def generate_case(feature,api_name):
    case =f"""

import allure
import pytest
from common.utils.read_util import ReadFileData
from operation.user import UserRequests



@allure.feature("获取用户信息模块")
@allure.severity(allure.severity_level.TRIVIAL)
@pytest.mark.weibo
class TestGetUserInfo():

    @pytest.mark.run(order=1)
    @allure.story("用例--获取单个用户信息")
    @allure.description("该用例针对获取单个学生信息接口的测试")
    @allure.issue("https://hbtown.worktile.com/mission/projects/5e43570808d6f034fafec9b4", name="点击，跳转到对应BUG的链接地址")
    @allure.testcase("https://www.processon.com/diagrams", name="点击，跳转到对应用例的链接地址")
    @pytest.mark.parametrize("username,except_result, except_code, except_msg",
                             ReadFileData().load_yaml("api_test_data.yml")["test_access_to_all_students"])
    def test_access_to_all_students(self, log, ip, username, except_result, except_code, except_msg):
        log.info("*************** 查询单个用户信息开始执行用例 ***************")
        result = UserRequests(log, ip).get_access_to_all_students(username)
        assert result.response.status_code == 200
        assert result.success == except_result, result.json()["msg"]
        log.info("code ==>> 期望结果：， 实际结果：".format(except_code, result.response.json().get("error_code")))
        assert result.response.json().get("error_code") == except_code
        # assert except_msg in str(result.json()["stu_info"])
        log.info("*************** 查询单个用户信息结束执行用例 ***************")
"""

    # 创建模块目录
    feature_path = get_case_dir(feature)
    mkdir(feature_path)

    # 创建用例文件
    test_path = os.path.join(feature_path, "test_{}.py".format(api_name.split(".")[0]))
    create_file(test_path, case)


def gen_all_case():
    """ 生成 configs/all_api.yml 内所有 case
    """
    all_api = ReadFileData().load_yaml("all_api.yml",)
    for k, v in all_api.items():
        # print(k,v)
        if v:  # 判断 feature 不为空
            api_items = str(v).split(" ")
            for api in api_items:
                generate_case(k, api)


if __name__ == '__main__':
    generate_case("test_huohuo","nishuone")
    # gen_all_case()